<html>
<head><meta charset="utf-8"><title>Manual Drop Implementation · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Manual.20Drop.20Implementation.html">Manual Drop Implementation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231705825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Manual%20Drop%20Implementation/near/231705825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Manual.20Drop.20Implementation.html#231705825">(Mar 24 2021 at 20:48)</a>:</h4>
<p>I have a struct with an <code>UnsafeCell&lt;MaybeUninit&lt;T&gt;&gt;</code> and some other fields and want <code>drop</code> to be called on T.  I'm not entirely sure what happens when the default drop implementation is called, but I believe that <code>UnsafeCell</code>s <code>drop</code> is called, which calls <code>drop</code> of <code>MaybeUninit</code>, which doesn't call <code>T</code>s drop method. I want to ensure that <code>T</code>s drop method is called. Since <code>Drop::drop</code> takes a mutable reference to self I cannot move out of the field that contains the <code>UnsafeCell</code>. Can anybody tell me how one can drop <code>T</code> and the other fields of the struct manually?</p>



<a name="231706104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Manual%20Drop%20Implementation/near/231706104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Manual.20Drop.20Implementation.html#231706104">(Mar 24 2021 at 20:50)</a>:</h4>
<p>get a pointer and call <code>ptr::drop_in_place</code></p>



<a name="231707710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Manual%20Drop%20Implementation/near/231707710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Manual.20Drop.20Implementation.html#231707710">(Mar 24 2021 at 21:01)</a>:</h4>
<p>Thanks</p>



<a name="232113237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Manual%20Drop%20Implementation/near/232113237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Manual.20Drop.20Implementation.html#232113237">(Mar 27 2021 at 19:43)</a>:</h4>
<p><span class="user-mention" data-user-id="328097">@BN</span> I've added <a href="https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html#method.assume_init_drop"><code>.assume_init_drop()</code></a> for this a while ago. But it's still unstable.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>